package edu.geo.containers;

import java.io.CharArrayWriter;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;

import edu.geo.containers.geomdomain.GeometricDomainFault;
import edu.geo.protocol.Properties;

public class GeoJSonHandler {
	
	public static String asGeoJson( int id, List<GeometricDomainFault> faults )
	{
		  Json jsonFeatures= new Json(); 	
		  jsonFeatures.setName("GeometricDomainFeatures");
		  jsonFeatures.setType("FeatureCollection");
		  ArrayList<Json> featureList= new ArrayList<Json>();
		  
		if(faults!=null)
		{
			for(GeometricDomainFault point:faults)
			{
				Json feature = new Json();
	    		 feature.setType("Feature");
	    		 GeoJson geoJson= new GeoJson();
				 geoJson.setType("Point");
				 ArrayList<Double> coordinates = new ArrayList<Double>();
				 coordinates.add(point.getLocation().getX());
				 coordinates.add(point.getLocation().getY());
				 geoJson.setCoordinates(coordinates);
				 feature.setGeometry(geoJson);
				 Properties prp = new Properties();
				 prp.setId(point.getType().toString());
				 prp.setResult(point.getType().getDescription());
				 feature.setProperties(prp);
				 featureList.add(feature);						
			}
		}
	  
		jsonFeatures.setFeatures(featureList);
	     return printGjson(jsonFeatures);
		
	}
	
	public static String printGjson(Json json)
	{
		String strJson= null;
		
		ObjectMapper jsonMapper = new ObjectMapper();
		CharArrayWriter writer = new CharArrayWriter();
		try
		{
			jsonMapper.writeValue(writer, json);
			strJson=writer.toString();
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return strJson;
	}

}
